package com.cloudcross.ssp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.ClientAccount;
import com.cloudcross.ssp.model.Resources;
import com.cloudcross.ssp.pulgin.mybatis.plugin.PageView;
import com.cloudcross.ssp.service.IAccountService;

@Service
public class AccountService implements IAccountService {

    public AccountService() {
    }

    @Autowired
    private GenericIBatisDao myBatisDao;

    @Override
    public PageView query(PageView pageView, Account t) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("paging", pageView);
        map.put("t", t);
        List<Account> list = myBatisDao.getList("accountSqlMapper.query", map);
        pageView.setRecords(list);
        return pageView;
    }

    @Override
    public String queryIconPathById(int id) {
        Account account = myBatisDao.get("accountSqlMapper.queryIconPathById", id);
        return account.getIconPathurl();
    }

    @Override
    public List<Account> queryAll(Account t) {
        return myBatisDao.getList("accountSqlMapper.queryAll", t);
    }

    @Override
    public void delete(String id) throws Exception {
        myBatisDao.save("accountSqlMapper.delete", id);
    }

    @Override
    public void update(Account t) throws Exception {
        myBatisDao.save("accountSqlMapper.update", t);
    }

    @Override
    public Account getById(String id) {
        return myBatisDao.get("accountSqlMapper.getById", id);
    }

    @Override
    public void add(Account account) throws Exception {
        myBatisDao.save("accountSqlMapper.add", account);
    }

    @Override
    public int countByParams(Map<String, Object> paramMap) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("key", paramMap.get("searchKey"));
        paramMap.put("value", searchValue);
        return myBatisDao.get("accountSqlMapper.countByParams", paramMap);
    }

    @Override
    public List<Account> findByParams(Map<String, Object> paramMap, Pager pager) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("key", paramMap.get("searchKey"));
        paramMap.put("value", searchValue);
        paramMap.put("pager", pager);
        return myBatisDao.getList("accountSqlMapper.selectByParams", paramMap);
    }

    @Override
    public Account isExist(String accountName) {
        return myBatisDao.get("accountSqlMapper.isExist", accountName);
    }

    @Override
    public boolean addAccount(Account account) {
        myBatisDao.save("accountSqlMapper.addAccount", account);
        return true;
    }

    @Override
    public Account countAccount(Account account) {
        return myBatisDao.get("accountSqlMapper.countAccount", account);
    }

    @Override
    public PageView queryNoMatch(Account account, PageView pageView) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("paging", pageView);
        map.put("t", account);
        List<Account> list = myBatisDao.getList("accountSqlMapper.queryNoMatch", account);
        pageView.setRecords(list);
        return pageView;
    }

    @Override
    public boolean updateState(List<Long> accountIdList, int state) {
        myBatisDao.save("accountSqlMapper.updateAccountStatus",
                MapBuilder.create(HashMap.class).add("accountIdList", accountIdList).add("modifyDate", new Date()).add("state", state).map());
        return true;
    }

    @Override
    public boolean editAccount(Account t) {
        myBatisDao.save("accountSqlMapper.editAccount", t);
        return true;
    }

    @Override
    public List<Account> findExcelAccounts(HashMap<String, Object> paramMap) {
        return myBatisDao.getList("accountSqlMapper.findExcelAccount", paramMap);
    }

    @Override
    public boolean addAccountOperator(Account account) {
        myBatisDao.save("accountSqlMapper.addAccountOperator", account);
        return true;
    }

    @Override
    public boolean addAccountAdvertiser(Account account) {
        myBatisDao.save("accountSqlMapper.addAccountAdvertiser", account);
        return true;
    }

    @Override
    public int countByParamsOperator(Map<String, Object> paramMap) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("key", paramMap.get("searchKey"));
        paramMap.put("value", searchValue);
        return myBatisDao.get("accountSqlMapper.countByParamsOperator", paramMap);
    }

    @Override
    public int countByParamsAdvertiser(Map<String, Object> paramMap) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("key", paramMap.get("searchKey"));
        paramMap.put("value", searchValue);
        return myBatisDao.get("accountSqlMapper.countByParamsAdvertiser", paramMap);
    }

    @Override
    public List<Account> findByParamsOperator(Map<String, Object> paramMap, Pager pager) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("key", paramMap.get("searchKey"));
        paramMap.put("value", searchValue);
        paramMap.put("pager", pager);
        return myBatisDao.getList("accountSqlMapper.selectByParamsOperator", paramMap);
    }

    @Override
    public List<Account> findByParamsAdvertiser(Long advertiserId) {
        return myBatisDao.getList("accountSqlMapper.selectByParamsAdvertiser", advertiserId);
    }

    @Override
    public boolean editAccountOperator(Account t) {
        myBatisDao.save("accountSqlMapper.editAccountOperator", t);
        return true;
    }

    @Override
    public boolean editAccountAdvertiser(Account t) {
        myBatisDao.save("accountSqlMapper.editAccountAdvertiser", t);
        return true;
    }

    @Override
    public List<Account> findExcelAccountsAdvertiser(HashMap<String, Object> paramMap) {
        String searchValue = SqlHelper.doLike(paramMap.get("value"));
        paramMap.put("value", searchValue);
        return myBatisDao.getList("accountSqlMapper.findExcelAccountsAdvertiser", paramMap);
    }

    @Override
    public List<Account> findExcelAccountsOperator(HashMap<String, Object> paramMap) {
        return myBatisDao.getList("accountSqlMapper.findExcelAccountsOperator", paramMap);
    }

    // 权限管理
    // list页面数据条数
    @Override
    public int queryAccountCountByCondition(Map<String, Object> condition) {
        return myBatisDao.get("accountSqlMapper.queryAccountCountForList", condition);
    }

    // list页面数据加载及数据导出
    @Override
    public List<Account> queryAccountByCondition(Map<String, Object> condition, Pager pager) {
        if (pager == null) {
            return myBatisDao.getList("accountSqlMapper.queryAccountForListExp", condition);
        } else {
            condition.put("pager", pager);
            return myBatisDao.getList("accountSqlMapper.queryAccountForList", condition);
        }
    }

    // 添加账号
    @Override
    public boolean insertAccount(Account account) {
        myBatisDao.save("accountSqlMapper.insertAccount", account);
        return true;
    }

    // 修改账号
    @Override
    public boolean updateAccount(Account account) {
        myBatisDao.save("accountSqlMapper.updateAccount", account);
        return true;
    }

    // 根据ID查询AccountId查询账号信息
    @Override
    public Account queryAccountById(Long id) {
        return myBatisDao.get("accountSqlMapper.queryAccountById", id);
    }

    @Override
    public Account querySingleAccount(String accountName) {
        return myBatisDao.get("accountSqlMapper.querySingleAccount", accountName);
    }

    // 客户端账号管理数据数目
    @Override
    public int queryClientAccountCountByCondition(Map<String, Object> condition) {
        return myBatisDao.get("accountSqlMapper.queryClientAccountCountByCondition", condition);
    }

    // 客户端账号管理数据
    @Override
    public List<ClientAccount> queryClientAccountByCondition(Map<String, Object> condition, Pager pager) {
        if (pager == null) {
            return myBatisDao.getList("accountSqlMapper.queryClientAccountExpByCondition", condition);
        } else {
            condition.put("pager", pager);
            return myBatisDao.getList("accountSqlMapper.queryClientAccountByCondition", condition);
        }
    }

    @Override
    public List<Resources> findRolesResources(Long roleId) {
        return myBatisDao.getList("accountSqlMapper.selectRolesResources", roleId);
    }

    public List<Account> list() {
        return myBatisDao.getList("accountSqlMapper.list");
    }
}
